package com.happy.study.servier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.study.dao.OptionMapper;
import com.happy.study.entity.OptionEntity;
import com.happy.study.servier.IOptionService;
import com.happy.study.util.StateEnum;
import com.happy.study.util.StateException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Wuzh
 * @Description:
 * @Date Created in 17:26 2021/1/31
 * @return
 */
@Service
public class OptionServiceImpl extends ServiceImpl<OptionMapper, OptionEntity> implements IOptionService {

    @Override
    public List<OptionEntity> queryList (OptionEntity optionEntity) {
        QueryWrapper<OptionEntity> queryWrapper = new QueryWrapper<> ();
        if ( optionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( optionEntity.getOptionId () != null ) {
            queryWrapper.eq ( "optionId", optionEntity.getOptionContent () );
        }
        if ( optionEntity.getSubjectId () != null ) {
            queryWrapper.eq ( "subjectId", optionEntity.getSubjectId () );
        }
        List<OptionEntity> optionEntities = baseMapper.selectList ( queryWrapper );
        return optionEntities;
    }

    @Override
    public OptionEntity querySingle (OptionEntity optionEntity) {
        QueryWrapper<OptionEntity> queryWrapper = new QueryWrapper<> ();
        if ( optionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( optionEntity.getOptionId () != null ) {
            queryWrapper.eq ( "optionId", optionEntity.getOptionContent () );
        }
        if ( optionEntity.getSubjectId () != null ) {
            queryWrapper.eq ( "subjectId", optionEntity.getSubjectId () );
        }
        optionEntity = baseMapper.selectOne ( queryWrapper );
        return optionEntity;
    }

    @Override
    public OptionEntity addSingle (OptionEntity optionEntity) {
        if ( optionEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        int insert = baseMapper.insert ( optionEntity );
        if ( insert == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return optionEntity;
    }
}
